import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;

import Messages.DiscoveryMessage;

// Represent Agent in the view 
public class MonitorController 
{
	private MonitorView m_view;
	private MonitorModel m_model;
	
	public MonitorController(MonitorView view, MonitorModel model)
	{
		m_view = view;
		m_view.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		m_view.addWindowListener(new WindowListener() 
		{	
			@Override
			public void windowClosed(WindowEvent arg0) 
			{
				// TODO Auto-generated method stub	
			}

			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub	
			}

			@Override
			public void windowClosing(WindowEvent e) {
				m_view.UnregisterAllMonitorData();
				m_model.Stop();
				m_view.dispose();
				System.exit(0);
				// TODO Auto-generated method stub
			}

			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub
			}

			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub
			}

			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub
			}

			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub
			}
		});
		m_view.addRegisterRequestListener(new IRegisterRequestListener() 
		{	
			@Override
			public void Unregister(MonitorData monitorData) 
			{
				m_model.UnregisterMonitorData(monitorData);
			}
			
			@Override
			public void Register(MonitorData monitorData) 
			{
				m_model.RegisterMonitorData(monitorData);
			}
		});
		m_model = model;
		m_model.AddNewAgentHandler(new INewAgentHandler() 
		{	
			@Override
			public void HandleNewAgent(Agent agent) 
			{
				m_view.AddAgent(agent);
			}
		});
		m_model.AddMonitorDataHandler(new IMonitorDataHandler() {
			
			@Override
			public void HandleMonitorData(MonitorData monitorData) 
			{
				m_view.HandleMonitorData(monitorData);
			}
		});
		m_model.AddAgentTimeoutHandler(new IAgentTimeoutHandler() {
			
			@Override
			public void HandleAgentTimeout(String host) 
			{
				m_view.HandleAgentTimeout(host);
			}
		});
		
		m_model.Start();
	}
}
